﻿package mortal.game.view.guild.otherpanel
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildShopBuyItemWin extends SmallWindow
    {
        private var _shopItem:BaseItem;
        private var _itemName:GTextFiled;
        private var _price:GTextFiled;
        private var _totalPrice:GTextFiled;
        private var _notEnougthMoney:GTextFiled;
        private var _ownMoney:GTextFiled;
        private var _capcity:GTextFiled;
        private var _btnBuy:GLoadingButton;
        private var _numInput:GNumericStepper;
        private var _sguildShopItem:SGuildShopItem;
        private var _tGuildShopItem:TGuildShopSell;
        private static var _npcId:int;
        private static var _instance:GuildShopBuyItemWin;

        public function GuildShopBuyItemWin(param1:ILayer = null)
        {
            if (_instance != null)
            {
                throw new Error(" GuildBuyItemWin 单例 ");
            }
            this.isHideDispose = false;
            return;
        }// end function

        public function showWin(param1:SGuildShopItem) : void
        {
            this._sguildShopItem = param1;
            this._tGuildShopItem = GuildShopConfig.instance.getGuildShopSellByCode(this._sguildShopItem.itemCode);
            setSize(270, 210);
            this.show();
            this.setInfo();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30075), 109, 68, 40, 24, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30078), 109, 122, 60, 24, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30077), 109, 170, 60, 24, this, GlobalStyle.textFormatItemGreen));
            this.pushUIToDisposeVec(UIFactory.gTextField("" + Language.getString(71224), 109, 150, 60, 24, this, GlobalStyle.textFormatItemGreen));
            this._shopItem = UICompomentPool.getUICompoment(BaseItem);
            this._shopItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            this._shopItem.x = 17;
            this._shopItem.y = 51;
            var _loc_2:Boolean = false;
            this._shopItem.isDropAble = false;
            this._shopItem.isDragAble = _loc_2;
            this.addChild(this._shopItem);
            var _loc_1:* = GlobalStyle.textFormatPutong;
            _loc_1.size = 15;
            this._itemName = UIFactory.gTextField("", 109, 44, 150, 25, this, _loc_1, true);
            _loc_1 = GlobalStyle.textFormatBai;
            _loc_1.align = AlignMode.LEFT;
            this._price = UIFactory.gTextField("", 140, 69, 150, 20, this, _loc_1, true);
            _loc_1 = GlobalStyle.textFormatHuang;
            _loc_1.align = AlignMode.LEFT;
            this._totalPrice = UIFactory.gTextField("", 140, 123, 150, 20, this, _loc_1, true);
            this._notEnougthMoney = UIFactory.gTextField("", 210, 123, 80, 20, this, GlobalStyle.textFormatHong);
            _loc_1 = GlobalStyle.textFormatItemGreen;
            _loc_1.align = AlignMode.LEFT;
            this._ownMoney = UIFactory.gTextField("", 170, 150, 60, 20, this, _loc_1, true);
            this._capcity = UIFactory.gTextField("", 170, 170, 60, 20, this, _loc_1, true);
            this._btnBuy = UIFactory.gLoadingButton(ResFileConst.ShopBuy, 211, 97, 49, 23, this);
            this._btnBuy.drawNow();
            this._btnBuy.configEventListener(MouseEvent.CLICK, this.clickHandler);
            _loc_1 = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.CENTER;
            this._numInput = UIFactory.gNumericStepper(112, 98, 50, 20, this, 99, 1, "NumericStepper", _loc_1, GNumericStepper.SetMaxAndMinNum);
            this._numInput.value = 1;
            this._numInput.configEventListener(Event.CHANGE, this.changeNum);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._shopItem.dispose(param1);
            this._itemName.dispose(param1);
            this._price.dispose(param1);
            this._totalPrice.dispose(param1);
            this._ownMoney.dispose(param1);
            this._capcity.dispose(param1);
            this._btnBuy.dispose(param1);
            this._numInput.dispose(param1);
            this._notEnougthMoney.dispose(param1);
            this._shopItem = null;
            this._itemName = null;
            this._price = null;
            this._totalPrice = null;
            this._ownMoney = null;
            this._capcity = null;
            this._btnBuy = null;
            this._numInput = null;
            this._notEnougthMoney = null;
            return;
        }// end function

        private function setInfo() : void
        {
            this._shopItem.itemData = new ItemData(this._tGuildShopItem.itemCode);
            this._itemName.htmlText = ItemsUtil.getItemName(this._shopItem.itemData);
            this._price.text = this._tGuildShopItem.price + "" + Language.getString(71225);
            this._totalPrice.text = this._tGuildShopItem.price.toString();
            this.updateCapcity();
            this.updateDonation();
            this._numInput.value = 1;
            this._numInput.maximum = this._sguildShopItem.buyLimit > this._sguildShopItem.leftAmount ? (this._sguildShopItem.leftAmount) : (this._sguildShopItem.buyLimit);
            this.changeNum();
            return;
        }// end function

        private function changeNum(event:Event = null) : void
        {
            var _loc_2:GTextFormat = null;
            this._totalPrice.text = this._tGuildShopItem.price * this._numInput.value + Language.getString(71225);
            if (this._tGuildShopItem.price * this._numInput.value > int(this._ownMoney.text))
            {
                _loc_2 = GlobalStyle.textFormatHong;
                _loc_2.align = AlignMode.LEFT;
                this._totalPrice.setTextFormat(_loc_2);
                this._notEnougthMoney.text = Language.getString(30079);
            }
            else
            {
                _loc_2 = GlobalStyle.textFormatHuang;
                _loc_2.align = AlignMode.LEFT;
                this._totalPrice.setTextFormat(_loc_2);
                this._notEnougthMoney.text = "";
            }
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (event.target == this._btnBuy)
            {
                if (this._sguildShopItem.leftAmount == 0 || this._sguildShopItem.leftAmount < this._numInput.value)
                {
                    MsgManager.showMouseMsg("" + Language.getString(71226), MsgRollTipsType.msgRollTips3);
                    return;
                }
                if (this._sguildShopItem.buyLimit == 0 || this._sguildShopItem.buyLimit < this._numInput.value)
                {
                    MsgManager.showMouseMsg("" + Language.getString(71227), MsgRollTipsType.msgRollTips3);
                    return;
                }
                if (int(this._ownMoney.text) < this._tGuildShopItem.price * this._numInput.value)
                {
                    MsgManager.showMouseMsg("" + Language.getString(71228), MsgRollTipsType.msgRollTips3);
                    return;
                }
                GameProxy.guild.buyGuildShopItem(this._tGuildShopItem.itemCode, this._numInput.value);
            }
            return;
        }// end function

        private function updateCapcity(param1:Object = null) : void
        {
            this._capcity.text = String(Cache.instance.pack.backPackCache.capacity - Cache.instance.pack.backPackCache.itemLength) + Language.getString(71229);
            return;
        }// end function

        private function updateDonation(param1:Object = null) : void
        {
            this._ownMoney.text = Cache.instance.guild.selfGuildInfo.selfInfo.contribution.toString();
            this.changeNum();
            return;
        }// end function

        private function updateGuildShopItem(param1:Object) : void
        {
            this._numInput.maximum = this._sguildShopItem.buyLimit > this._sguildShopItem.leftAmount ? (this._sguildShopItem.leftAmount) : (this._sguildShopItem.buyLimit);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show();
            NetDispatcher.addCmdListener(ServerCommand.GuildShopGetItem, this.updateGuildShopItem);
            NetDispatcher.addCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.updateDonation);
            NetDispatcher.addCmdListener(ServerCommand.UpdateCapacity, this.updateCapcity);
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            NetDispatcher.removeCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.updateDonation);
            NetDispatcher.removeCmdListener(ServerCommand.UpdateCapacity, this.updateCapcity);
            NetDispatcher.removeCmdListener(ServerCommand.GuildShopGetItem, this.updateGuildShopItem);
            return;
        }// end function

        public static function get instance() : GuildShopBuyItemWin
        {
            if (_instance == null)
            {
                _instance = new GuildShopBuyItemWin;
            }
            return _instance;
        }// end function

    }
}
